import { httpClient } from '@/utils/http/axios';
import { ErrorMessageMode } from '#/axios';
import { AjaxResult } from '@/api/model';
import { PageRequest, PageData } from '@/utils/common/types';
import {
  SheetInputDto,
  SheetOutputDto,
  SheetOutputDtoSimple,
  SheetSetRoleDto,
  SheetSetUserDto,
} from './model';
import { UserNode } from '@/api/identity/admin/user/model';
import { RoleNode } from '@/api/identity/admin/role/model';

/** 报表表格模块URL枚举 */
enum Api {
  /** 读取表格报表简单信息 */
  ReadSimple = '/api/report/admin/sheet/readSimple',
  /** 读取表格报表 */
  Read = '/api/report/admin/sheet/read',
  /** 创建表格报表 */
  Create = '/api/report/admin/sheet/create',
  /** 更新表格报表 */
  Update = '/api/report/admin/sheet/update',
  /** 删除表格报表 */
  Delete = '/api/report/admin/sheet/delete',

  /** 读取表格报表[角色]树 */
  ReadRoles = '/api/report/admin/sheet/readRoles',
  /** 读取表格报表[用户]树 */
  ReadUsers = '/api/report/admin/sheet/readUsers',

  /** 设置表格报表角色 */
  SetRoles = '/api/report/admin/sheet/setRoles',
  /** 设置表格报表用户 */
  SetUsers = '/api/report/admin/sheet/setUsers',
}

//#region 读取表格报表简单信息
/**
 * 读取表格报表简单信息
 * @param request 分页请求
 * @param mode 错误消息模型
 */
export function readSheetSimple(request: PageRequest, mode: ErrorMessageMode = 'message') {
  return httpClient.post<PageData<SheetOutputDtoSimple>>(
    {
      url: Api.ReadSimple,
      params: request,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
//#region 读取表格报表
/**
 * 读取表格报表
 * @param request 分页请求
 * @param mode 错误消息模型
 */
export function readSheet(request: PageRequest, mode: ErrorMessageMode = 'message') {
  return httpClient.post<PageData<SheetOutputDto>>(
    {
      url: Api.Read,
      params: request,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
//#region 创建表格报表
/**
 * 创建表格报表
 * @param dtos 输入DTO
 * @param mode 错误消息模型
 */
export function createSheet(dtos: SheetInputDto[], mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.Create,
      params: dtos,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
//#region 更新表格报表
/**
 * 更新表格报表
 * @param dtos 输入DTO
 * @param mode 错误消息模型
 */
export function updateSheet(dtos: SheetInputDto[], mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.Update,
      params: dtos,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
//#region 删除表格报表
/**
 * 删除表格报表
 * @param ids 表格报表编号
 * @param mode 错误消息模型
 */
export function deleteSheet(ids: (string | bigint)[], mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.Delete,
      params: ids,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 读取表格报表[角色]树
/**
 * 读取表格报表[角色]树
 * @param SheetId 表格编号
 * @param mode 错误消息模型
 */
export function readSheetRoles(SheetId: string | bigint, mode: ErrorMessageMode = 'message') {
  return httpClient.get<RoleNode[]>(
    {
      url: Api.ReadRoles,
      params: { SheetId: SheetId },
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
//#region 读取表格报表[用户]树数据
/**
 * 读取表格报表[用户]树数据
 * @param SheetId 表格编号
 * @param mode 错误消息模型
 */
export function readSheetUsers(SheetId: string | bigint, mode: ErrorMessageMode = 'message') {
  return httpClient.get<UserNode[]>(
    {
      url: Api.ReadUsers,
      params: { SheetId: SheetId },
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 设置表格报表角色
/**
 * 设置表格报表角色
 * @param dtos 表格角色信息
 * @param mode 错误消息模型
 */
export function setSheetRoles(dtos: SheetSetRoleDto, mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.SetRoles,
      params: dtos,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
//#region 设置表格报表用户
/**
 * 设置表格报表用户
 * @param dtos 表格用户信息
 * @param mode 错误消息模型
 */
export function setSheetUsers(dtos: SheetSetUserDto, mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.SetUsers,
      params: dtos,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
